package leetcode_题库;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Ding
 * @create 2022/8/19 9:43
 */
public class _167_两数之和2_输入有序数组 {
    public static void main(String[] args) {
        int[] nums = new int[]{-1,0};
        int target = -1;

        System.err.println(Arrays.toString(getSumArray(nums, target)));
    }

    private static int[] getSumArray(int[] nums, int target) {
        int lp = 0;
        int rp = nums.length - 1;
        //双指针，如果左指针小于右指针，循环成立
        while (lp < rp){
            if (nums[lp] + nums[rp] == target){
                //相等，返回角标数组（因为角标从1开始，看题干，故+1）
                return new int[]{lp+1,rp+1};
            }else if (nums[lp] + nums[rp] > target){
                //如果值比目标值大了，就让右指针左移，缩小范围
                rp--;
            }else{
                //如果值比目标值小，让左指针右移，缩小范围
                lp++;
            }
        }
        return new int[0];

    }

    private static int[] getFuckSumArray(int[] nums, int target) {
        int lp = 0;
        int rp = nums.length - 1;

        while (lp < nums.length - 1) {
            while (lp < rp) {
                if (nums[lp] + nums[rp] == target) {
                    return new int[]{lp + 1, rp + 1};
                }
                rp--;
            }
            rp = nums.length - 1;
            lp++;
        }
        return new int[0];
        /*

            > 2022/08/19 09:57:53
            解答成功:
                执行耗时:637 ms,击败了5.00% 的Java用户
                内存消耗:44.5 MB,击败了12.99% 的Java用户

        */
    }
}
